package org.richin.lang.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.richin.date.util.DateFormatUtils;
import org.richin.date.util.DateUtils;

public class RandomUtil extends RandomStringUtils {
	 public static final Random RANDOM = new Random();
	/**
	 * 获取length长度的随机串,由随机表组成
	 * @param randomTables 随机表
	 * @param length 需要生成的随机串长度
	 * @return 随机串
	 */
	public static String getRandomString(char[] randomTable,int length){
		StringBuilder sb = new StringBuilder();
		int l=randomTable.length;
		for (int i = 0; i < length; i++) {
			sb.append(randomTable[RANDOM.nextInt(l)]);
		}
		return sb.toString();
	}
	
	/**
	 * 得到一个按日期格式的自动编号(长度为18位)<br>
	 * 如 yyyyMMddHHmmss+四位随机码
	 * @return String
	 */
	public static String getAutoId() {
		return DateFormatUtils.ORA_DATE_TIME_ALL__FORMAT.format(new Date())+ (Math.round(Math.random() * 8999) + 1000);
	}
	
	public static int getRandomNum(int max){
		int num=RANDOM.nextInt()%max;
		return num<0?-num:num;
	}
	/**
	 * 生成随机文件名
	 */
	public static String generateRandomFilename() {
		String RandomFilename = "";
		Random rand = new Random();//生成随机数     
		int random = rand.nextInt();

		Calendar calCurrent = Calendar.getInstance();
		int intDay = calCurrent.get(Calendar.DATE);
		int intMonth = calCurrent.get(Calendar.MONTH) + 1;
		int intYear = calCurrent.get(Calendar.YEAR);
		String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" + String.valueOf(intDay) + "_";
		System.out.println("生成于今日的文件名前缀为：" + now);

		RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random) + ".";

		return RandomFilename;
	} //System.out.println(generateRandomFilename());

	public static String randomString(int length) {
		return randomString(length, true);
	}

	public static String randomString(int length, boolean includeNumbers) {
		StringBuffer b = new StringBuffer(length);
		for (int i = 0; i < length; i++)
			if (includeNumbers)
				b.append(randomCharacter());
			else
				b.append(randomAlpha());

		return b.toString();
	}

	public static char randomCharacter() {
		int i = (int) (Math.random() * 3D);
		if (i < 2)
			return randomAlpha();
		else
			return randomDigit();
	}

	public static char randomAlpha() {
		int i = (int) (Math.random() * 52D);
		if (i > 25)
			return (char) ((97 + i) - 26);
		else
			return (char) (65 + i);
	}

	public static char randomDigit() {
		int i = (int) (Math.random() * 10D);
		return (char) (48 + i);
	}
	/**
	 * 产生1～9无重复随机数 
	 */
public static void sdf(){
	  int [] seed = {1,2,3,4,5,6,7,8,9};   
      int [] ranArr = new int [9];   
      Random ran = new Random();   
      for(int i = 0 ; i<seed.length ; i++){   
          int j = ran.nextInt(seed.length-i);   
          ranArr [i] = seed [j];   
          seed [j] = seed [seed.length-1-i];   
          System.out.println ("ranArr[" + i + "]" + ":" + ranArr[i]);   
      }   
}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DateFormat format = new SimpleDateFormat("yyMMddhhmm");
		System.out.println(format.format(new Date()));
		// TODO Auto-generated method stub
		System.out.println("s" + generateRandomFilename());
		System.out.println(randomString(6, false));
		System.out.println(RandomStringUtils.randomNumeric(6));
	}

}
